package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * 条件分页查询频道数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();
        //分页 + 频道名称模糊匹配查询
        IPage ipage = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(dto.getName())) {
            lqw.like(WmChannel::getName, dto.getName());
        }
        lqw.orderByDesc(WmChannel::getCreatedTime);
        page(ipage, lqw);

        //结果返回
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setCurrentPage((int) ipage.getCurrent());
        pageResponseResult.setSize((int) ipage.getSize());
        pageResponseResult.setTotal((int) ipage.getTotal());
        pageResponseResult.setData(ipage.getRecords());

        return pageResponseResult;
    }

    /**
     * 新增频道数据
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult insert(WmChannel wmChannel) {
        //1.入参校验
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.校验频道名称是否重复
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmChannel::getName, wmChannel.getName());
        WmChannel dbChannel = getOne(lqw);
        if (dbChannel != null) {
            //频道已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已经存在");
        }

        //插入数据
        wmChannel.setIsDefault(true); // 默认为默认频道
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据频道id删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        //1入参校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断id对应的频道是否存在
        WmChannel dbChannel = getById(id);
        if (dbChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在!");
        }
        //3.只有禁用的频道才能删除
        if (dbChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "启用中的频道不可删除!");
        }
        //4.查看频道是否关联文章
        Integer count = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getChannelId, id) // 被发布中的文章引用的频道
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "当前频道已关联文章,不可删除!");
        }
        //5.删除
        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel dto) {
        //1.入参校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.校验频道状态 如果频道被引用则不能禁用
        Integer count = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getChannelId, dto.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "当前频道已关联文章,暂时不可修改!");
        }
        dto.setIsDefault(true); // 是否默认频道
        //3.修改频道
        updateById(dto);

        //4.返回数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}